/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <clptcPipCmd.h>
#include <queObj.h>
#include <clptcDlgItf.h>
#include <clptcCmdList.h>
#include <clptcHostDoCmd.h>
#include <hexstr.h>

#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
#if CLPTC_CPL_HOST_ENABLE
static cPipCmdMgrStc clptcPipCmdIst;
int clptcPipCmdHello(void *cmdData, void *usrData)
{
    logdebugc("hello:%s", (const char *)cmdData);
    return 1;
}
int clptcPipCmdHelp(void *cmdData, void *usrData)
{
    cPipCmdMgrStc *tmpMgr = (cPipCmdMgrStc *)usrData;
    cStrCmdMgrPrtCmd(&tmpMgr->cmdMgr);
    return 1;
}
int clptcPipCmdExit(void *cmdData, void *usrData)
{
    logdebugc("do exit");
    exit(-1);
    return 1;
}
int clptcPipCmdAdd(void *cmdData, void *usrData)
{
    int a, b;
    if (!cmdData)
    {
        logerrorc("");
        return -1;
    }
    int tmplen = sscanf((const char *)cmdData, "%d %d", &a, &b);
    if (tmplen < 2)
    {
        logerrorc("add need 2 param!");
        return -1;
    }
    logdebugc("input:[%s], get len:%d, val:%d+%d=%d", (const char *)cmdData, tmplen, a, b, a + b);
    return 1;
}
int clptcPipCmdClptcCmd(void *cmdData, void *usrData)
{
    if (!(cmdData && usrData))
    {
        logerrorc("need input param like: echo clpcmd 0");
        return -1;
    }
    cPipCmdMgrStc *tmpMgr = (cPipCmdMgrStc *)usrData;
    clptcHostDoCmdOpt *tmpCmdOpt = (clptcHostDoCmdOpt *)tmpMgr->usrData;
    int tmpcmdId;
    logdebugc("cmd data:%s", (const char *)cmdData);
    sscanf((const char *)cmdData, "%d", &tmpcmdId);
    switch (tmpcmdId)
    {
    case clptcCmdEnum_rwSn:
    case 0xff00: // config addrs 1(1 2 3 4)
    case 0xff01: // config addrs 2(0 1 2 3)
    {
        // cmd only
        clptcHostCmdData tmpcmdData;
        tmpcmdData.cmdid = tmpcmdId;
        logdebugc("input cmd only:%d", tmpcmdData.cmdid);
        queObjWrite(tmpCmdOpt->cmdFifo, &tmpcmdData);
        break;
    }
    case clptcCmdEnum_getInf:
    case clptcCmdEnum_heartBeat:
    case clptcCmdEnum_wrtFileBeg:
    case clptcCmdEnum_readFileBeg:
    {
        // cmd with addr
        int tmpAddr;
        int tmplen = sscanf((const char *)cmdData, "%d %d", &tmpcmdId, &tmpAddr);
        if (tmplen != 2)
        {
            logdebugc("cmd wr addr need cmdid.addr");
            return -1;
        }
        clptcHostCmdData tmpcmdData;
        tmpcmdData.cmdid = tmpcmdId;
        tmpcmdData.cmddt[0] = tmpAddr;
        logdebugc("input cmd whit addr:%d,%d", tmpcmdId, tmpcmdData.cmddt[0]);
        queObjWrite(tmpCmdOpt->cmdFifo, &tmpcmdData);
        break;
    }
    case clptcCmdEnum_rwAddr:
    {
        char tmpCfgSn[32];
        int tmpRwFlag;
        int tmpWaddr;
        int tmplen = sscanf((const char *)cmdData, "%d %32s %d %d", &tmpcmdId, tmpCfgSn, &tmpRwFlag, &tmpWaddr);
        if (tmplen != 4)
        {
            logdebugc("cmd wr addr need cmdid.sn.rwflag.addr");
            return -1;
        }
        clptcHostCmdData tmpcmdData;
        clptcCmdStcSnd_rwAddr *tmpRwAddr = (clptcCmdStcSnd_rwAddr *)tmpcmdData.cmddt;
        cvtStr2hex(tmpCfgSn, sizeof(tmpCfgSn), tmpRwAddr->cfgSn, sizeof(tmpRwAddr->cfgSn));
        tmpRwAddr->rwflag = tmpRwFlag;
        tmpRwAddr->wAddr = tmpWaddr;
        tmpcmdData.cmdid = clptcCmdEnum_rwAddr;
        logdebugc("cmd rwaddr:%d,%d", tmpRwAddr->rwflag, tmpRwAddr->wAddr);
        logHexBufStr("rwaddr sn", tmpRwAddr->cfgSn, sizeof(tmpRwAddr->cfgSn));
        queObjWrite(tmpCmdOpt->cmdFifo, &tmpcmdData);
        break;
    }
    case clptcCmdEnum_wrtFileFin:
    {
        // show sub device info
        clptcDlgMgrStc *tmpDlgMgr = (clptcDlgMgrStc *)tmpCmdOpt->funptr->lpDlgMgr;
        for(uint8_t i = 0; i < tmpDlgMgr->dlgNbr; i++)
        {
            if(tmpDlgMgr->lpDlgs[i].dlgAddr == 0xff)
            {
                continue;
            }
            char tmpSnStr[32];
            cvtHex2str((const char *)tmpDlgMgr->lpDlgs[i].dlgSn, 16, tmpSnStr, 32);
            logdebugc("ver=%s,sn=%.*s,addr=%d,sta=%d;", tmpDlgMgr->lpDlgs[i].dlgVer, 
                32, tmpSnStr, tmpDlgMgr->lpDlgs[i].dlgAddr, tmpDlgMgr->lpDlgs[i].dlgSmSta);
        }
        break;
    }
    default:
    {
        logdebugc("input cmd not surpport:%d", tmpcmdId);
        break;
    }
    }
    return 1;
}
cStrCmdInfStc myChkCmdList[] = {
    {"hello", "hello check", clptcPipCmdHello},
    {"help", "print support command list", clptcPipCmdHelp},
    {"exit", "exit code", clptcPipCmdExit},
    {"add", "check add function", clptcPipCmdAdd},
    {"clpcmd", "input c light protocol comman", clptcPipCmdClptcCmd},
};

void clptcPipCmdInit(void *cfgusr)
{
    cPipCmdMgrInit(&clptcPipCmdIst, myChkCmdList, ARRAY_LEN(myChkCmdList), CLPTC_CHK_FILE_SAVE_DIR "cpipCmdChk.txt", cfgusr);
}
void clptcPipCmdUp()
{
    cPipCmdMgrUp(&clptcPipCmdIst);
}

#endif
#endif